<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>在Spacemacs中使用conda环境启用lsp-pyright - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="如果你是Emacs的用户，那么肯定听说过Spacemacs，以开箱即用而出名，界面也非常的漂亮。放弃了一段时间的Emacs，之前都是自己写的" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/emacs/%E5%9C%A8spacemacs%E4%B8%AD%E4%BD%BF%E7%94%A8conda%E7%8E%AF%E5%A2%83%E5%90%AF%E7%94%A8lsp-pyright/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="在Spacemacs中使用conda环境启用lsp-pyright" />
<meta property="og:description" content="如果你是Emacs的用户，那么肯定听说过Spacemacs，以开箱即用而出名，界面也非常的漂亮。放弃了一段时间的Emacs，之前都是自己写的" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/emacs/%E5%9C%A8spacemacs%E4%B8%AD%E4%BD%BF%E7%94%A8conda%E7%8E%AF%E5%A2%83%E5%90%AF%E7%94%A8lsp-pyright/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2023-06-19T22:15:08+08:00" />
<meta property="article:modified_time" content="2023-06-19T22:15:08+08:00" />

<meta itemprop="name" content="在Spacemacs中使用conda环境启用lsp-pyright">
<meta itemprop="description" content="如果你是Emacs的用户，那么肯定听说过Spacemacs，以开箱即用而出名，界面也非常的漂亮。放弃了一段时间的Emacs，之前都是自己写的"><meta itemprop="datePublished" content="2023-06-19T22:15:08+08:00" />
<meta itemprop="dateModified" content="2023-06-19T22:15:08+08:00" />
<meta itemprop="wordCount" content="1065">
<meta itemprop="keywords" content="Emacs,Spacemacs,Python," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="在Spacemacs中使用conda环境启用lsp-pyright"/>
<meta name="twitter:description" content="如果你是Emacs的用户，那么肯定听说过Spacemacs，以开箱即用而出名，界面也非常的漂亮。放弃了一段时间的Emacs，之前都是自己写的"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">在Spacemacs中使用conda环境启用lsp-pyright</h1>

      <div class="post-meta">
        <span class="post-time"> 2023-06-19 </span>
        <div class="post-category">
            <a href="/categories/%E7%BC%96%E8%BE%91%E5%99%A8/"> 编辑器 </a>
            <a href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/"> 编程语言 </a>
            </div>
          <span class="more-meta"> 约 1065 字 </span>
          <span class="more-meta"> 预计阅读 3 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents"></nav>
  </div>
</div>
    <div class="post-content">
      <p>如果你是Emacs的用户，那么肯定听说过Spacemacs，以开箱即用而出名，界面也非常的漂亮。放弃了一段时间的Emacs，之前都是自己写的配置，没有时间维护，便尝试了一下Spacemacs。</p>
<p>在使用Spacemacs开发Python的时候，启动<code>lsp-pyright</code>对Python的支持，<code>lsp-pyright</code>可以识别virtualenv虚拟环境，暂时还没有对conda env的支持。好在还是有办法设置python程序的路径。</p>
<p>实现方式用到了几个关键词方法：</p>
<ol>
<li>dotspacemacs-configuration-layers列表添加python、lsp以及conda</li>
<li>directory local variable</li>
<li>with-eval-after-load</li>
</ol>
<p>下面解释每个点的配置</p>
<h1 id="spacemacs-layers">Spacemacs layers</h1>
<p>启动python和lsp，这两个layer不需要做过多的设置。</p>
<p>启动conda的时候需要设置conda-anaconda-home变量，如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-lisp" data-lang="lisp"><span class="p">(</span><span class="nv">conda</span> <span class="ss">:variables</span> <span class="nv">conda-anaconda-home</span> <span class="s">&#34;D:/path/to/miniconda3&#34;</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><p>python解释器我使用的是miniconda，可以修改成自己对应的路径。</p>
<h1 id="directory-local-variable">Directory local variable</h1>
<p>Emacs支持在项目目录创建一个 <code>.dir-locals.el</code> 文件，来创建针对项目文件的本地变量，而不会覆盖全局的变量。这样多个项目之间就不会相互影响了。这块可以参考<a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html">官方文档</a>。</p>
<p>在我们的Python项目根目录下创建一个 <code>.dir-locals.el</code> 文件，写入以下内容：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-lisp" data-lang="lisp"><span class="p">((</span><span class="nv">python-mode</span> <span class="o">.</span> <span class="p">((</span><span class="nf">eval</span> <span class="o">.</span> <span class="p">(</span><span class="nv">with-eval-after-load</span> <span class="ss">&#39;lsp-pyright</span>
                           <span class="p">(</span><span class="k">progn</span>
                             <span class="p">(</span><span class="nv">lsp-register-custom-settings</span>
                              <span class="o">`</span><span class="p">((</span><span class="s">&#34;python.pythonPath&#34;</span> <span class="s">&#34;D:/path/to/miniconda3/envs/stringle_pro/python.exe&#34;</span><span class="p">))))</span>
                           <span class="p">)))))</span>
</code></pre></td></tr></table>
</div>
</div><ol>
<li>首先<code>python-mode</code> 指定只有在python模式下后面的代码才会执行；</li>
<li>(eval . func_call)是一个cons pair，eval函数相当于是标记后面的fanc_call是一个可以执行的S表达式；</li>
</ol>
<p>如果要设置指定模式下的变量值，则使用下面的代码：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-lisp" data-lang="lisp"><span class="p">((</span><span class="nv">python-mode</span> <span class="o">.</span> <span class="p">((</span><span class="nv">variable-name</span> <span class="nv">variable-value</span><span class="p">))))</span>
</code></pre></td></tr></table>
</div>
</div><h1 id="with-eval-after-load">with-eval-after-load</h1>
<p><code>with-eval-after-load</code> 是Emacs内置的一个宏，定义在 <code>subr.el</code>。</p>
<p>这个地方是很需要注意代码运行的顺序与时机的。在Emacs打开python代码的时候会调用 <code>lsp</code> 启动lsp-mode，lsp会询问使用的lsp后端，python的lsp后端有非常的多，pyls, mspy等。我选择的是lsp-pyright。然后lsp根据设置的python lsp后端，加载lsp-pyright的代码。</p>
<p>这时上面的with-eval-after-load就会起作用了，在加载完lsp-pyright.el的代码后就会执行with-eval-after-load里面的代码。把 <code>python.pythonPath</code> 这个lsp变量设置完成后。</p>
<p>lsp就会调用lsp-pyright里面lsp相关函数，启动pyright后端。</p>
<h1 id="总结">总结</h1>
<p>本文说明了如何在spacemacs下启用lsp来支持在conda env虚拟环境下开发python的配置工作，需要对Emacs和lsp的工作原理有一定的认识与了解。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2023-06-19
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/emacs/">Emacs</a>
          <a href="/tags/spacemacs/">Spacemacs</a>
          <a href="/tags/python/">Python</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/python/%E4%BD%BF%E7%94%A8websocket%E6%8A%80%E6%9C%AF%E5%AE%9E%E7%8E%B0chatgpt%E5%BA%94%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">使用WebSocket技术实现ChatGPT应用</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/python/python&#43;docker&#43;fastapi%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91%E9%83%A8%E7%BD%B2restful-api/">
            <span class="next-text nav-default">Python Docker FastAPI快速开发部署RESTful API</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
